#include "QNotebookListItemWidget.h"
#include "MNotebookListItemWidget.h"
#include "QvernoteView.h"
#include <MWidget>

QNotebookListItemWidget::QNotebookListItemWidget(bool isDeleteMode, int noteCount)
    : m_bDeleteMode(isDeleteMode), m_nNoteCount(noteCount)
{
    setCellViewType("NotebookListItem");
}

void 	QNotebookListItemWidget::updateCell (const QModelIndex &index, MWidget *cell) const
{
    QVariant checkData = index.data(Qt::CheckStateRole);
    Qt::CheckState checkState = (Qt::CheckState)checkData.value<int>();
    MNotebookListItemWidget* w = (MNotebookListItemWidget*)cell;

    if(checkState == Qt::Checked)
        w->lDeleteMark->setPixmap(QPixmap(":/IconAdd/widgets_tickmark_list.png"));
    else
        w->lDeleteMark->setPixmap(QPixmap());

    QVariant data = index.data(Qt::DisplayRole);
    int notebookptr = data.value<int>();
    Notebook* notebook = (Notebook*)(int*)notebookptr;
    QvernoteAPI* api = QvernoteAPI::Instance();
    //if(isDeleteMode)
    //	ui.lDeleteMark->setVisible(true);
    //else
    //	ui.lDeleteMark->setVisible(false);


    if(notebook->defaultNotebook)
    {
        w->lNotebookIcon->setVisible(true);
        w->lNotebookIcon->setPixmap(QPixmap(":/IconAdd/star-32.png"));
    }

    QStringList blacklistedNotebooks = QvernoteSettings::Instance()->getBlacklistedNotebooks().split(',');
    if(blacklistedNotebooks.contains(QString::fromStdString(notebook->guid)) && !notebook->guid.empty())
    {
            qDebug() << "Notebook is blacklisted: " << notebook->name.c_str();
            w->lNotebookIcon->setVisible(true);
            w->lNotebookIcon->setPixmap(QPixmap(":/IconAdd/general_presence_offline.png"));
    }

    w->lNotebookName->setText(QString::fromUtf8(notebook->name.c_str()));

    w->lNotesCount->setText(QString::number((m_nNoteCount == -1)? api->getNotebookContentsCount(*notebook) : m_nNoteCount, 10) + QApplication::trUtf8(" notes"));


    Tag tag;
    bool hasNextTag;
    QString tagList;
    for(hasNextTag = api->getFirstNotebookTag(notebook->name, tag); hasNextTag; hasNextTag = api->getNextNotebookTag(notebook->name, tag))
    {
            tagList += QString::fromUtf8(tag.name.c_str()) + " ";
    }

    if(tagList.size() == 0)
    {
            w->lNotebookTags->setText(QApplication::trUtf8("No tags"));
    }
    else
    {
            w->lNotebookTags->setText(tagList);
    }

}

QSizeF 	QNotebookListItemWidget::cellSize () const
{
        return QSizeF(800, 80);
}

QNotebookListItemWidget::~QNotebookListItemWidget()
{

}

/*void QNotebookListItemWidget::mark()
{
	if(m_bItemMarked)
	{
		m_bItemMarked = false;
		ui.lDeleteMark->clear();
		emit notebookUnmarked();
	}
	else
	{
		m_bItemMarked = true;
		ui.lDeleteMark->setPixmap(QPixmap(":/IconAdd/widgets_tickmark_list.png"));
		emit notebookMarked();
	}
}*/
